﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using Bestlife.ERP.DataAccess;

public partial class new_Bonus_Calculation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow rws in GridView1.Rows)
        {
            Session["user"] = null;
            string r_no = rws.Cells[0].Text;
            string D_id = rws.Cells[1].Text;
            Session["user"] = D_id;
            string price = rws.Cells[3].Text;
            string PV = rws.Cells[4].Text;
            string bv = rws.Cells[5].Text;
            string R_date = rws.Cells[6].Text;


            Allprocess(D_id, price, PV, bv);
        }
    }

    #region All process Methord

    public Boolean Allprocess(string Disids, string totp, string totpv, string totbv)
    {
        Boolean transact_Ok = false;
        Boolean Otheraccount = false;
        int count_ = 0;
        #region check if Vandana Or Bestlife Account

        if (Disids.Equals("vandana"))
        {
            Otheraccount = true;
        }
        else if (Disids.Equals("bestlife"))
        {
            Otheraccount = true;
        }
        #endregion



        if (Otheraccount == false)
        {


            //string u_name = Session["user"].ToString();

            #region Entities And DataAccess Class

            Bestlife.ERP.DataAccess.D_bonus bnus = new Bestlife.ERP.DataAccess.D_bonus();
            Bestlife.ERP.Entities.E_Bonus ebns = new Bestlife.ERP.Entities.E_Bonus();

            Bestlife.ERP.DataAccess.users Us = new Bestlife.ERP.DataAccess.users();

            Bestlife.ERP.Entities.E_UserPurchs eupchs = new Bestlife.ERP.Entities.E_UserPurchs();
            Bestlife.ERP.DataAccess.D_UserPurchase dupchs = new Bestlife.ERP.DataAccess.D_UserPurchase();


            #endregion

            string UId = Disids;

            //#region check bestlife or vandana
            //Boolean Otheraccount = false;

            //#endregion


            if (Us.GetUserTablest(UId))
            {

                Bestlife.ERP.Entities.E_Bonus bnsback = bnus.Getbonus(UId);

                #region assign values

                double pv = double.Parse(bnsback.Ppv.ToString());
                double bv = double.Parse(bnsback.Bv.ToString());
                double DbTprice = double.Parse(bnsback.T_sales.ToString());
                double DbTpv = double.Parse(bnsback.Tpv.ToString());
                double DbTbv = double.Parse(bnsback.Tbv.ToString());

                #endregion


                double sTp = DbTprice + double.Parse(totp);
                double sTpv = DbTpv + double.Parse(totpv);
                double sTbv = DbTbv + double.Parse(totbv);
                double sbv = bv + double.Parse(totbv);
                double spv = pv + double.Parse(totpv);

                #region Update Bonus table
                Us.UpdateBonus(UId, sTp, sTpv, sTbv, double.Parse(totpv), double.Parse(totbv));//send All Product Pv n Bvs only
                #endregion

                #region Send Values to calculation_Daily methord

                Bestlife.ERP.DataAccess.calculation_Daily cals = new Bestlife.ERP.DataAccess.calculation_Daily();

                // Pass Values to the calculation_Dailys class :
                cals.calculation_Dailys(UId, double.Parse(totbv), double.Parse(totpv), double.Parse(totp));


                #endregion

            }
            else
            {
                #region Insert To Bonus

                Bestlife.ERP.BusinessRules.Check_montlyPV checkmp = new Bestlife.ERP.BusinessRules.Check_montlyPV();
                int userst = checkmp.first_status(UId, double.Parse(totpv));

                ebns.User_id = UId;
                ebns.Tpv = decimal.Parse(totpv);
                ebns.Ppv = decimal.Parse(totpv);
                ebns.Cpv = decimal.Parse("0");
                ebns.CPV_month = decimal.Parse("0");
                ebns.T_sales = decimal.Parse(totp);
                ebns.T_commision = decimal.Parse("0");
                ebns.T_Lship = decimal.Parse("0");
                ebns.Subdealers_Id = "Not In Use";
                ebns.CPV_month = decimal.Parse("0");
                ebns.Bv = decimal.Parse(totbv);
                ebns.User_Status = userst.ToString();
                ebns.Tbv = decimal.Parse(totbv);
                ebns.Storebv = 0;
                ebns.Storepv = 0;
                ebns.Lship_Status = 0;
                ebns.Store_Lship = 0;
                ebns.Storecomm = 0;
                ebns.Usercreate = UId;
                ebns.Date_create = System.DateTime.Now;


                bool st = bnus.Bonus_Add(ebns);

                if (st)
                {
                    //msg insert ok
                }
                else
                {
                    // msg server or Sql Error 
                }


                #endregion

                #region Send Values to calculation_Daily methord

                Bestlife.ERP.DataAccess.calculation_Daily cals = new Bestlife.ERP.DataAccess.calculation_Daily();

                // Pass Values to the calculation_Dailys class :
                cals.calculation_Dailys(UId, double.Parse(totbv), double.Parse(totpv), double.Parse(totp));
                #endregion
            }
        }
        else 
        {
            count_++;
            Logas.Text = "Purchese as BestLife Or Vandana Count:  "+count_;
        }
        return transact_Ok;
    }
    #endregion


    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        List<Bestlife.ERP.Entities.E_Receipt> ridlist = new List<Bestlife.ERP.Entities.E_Receipt>();
        string Rids = "";
        DateTime dt= Calendar1.SelectedDate;
        string dateformat= dt.Year+"-"+dt.Month+"-"+dt.Day;

        Bestlife.ERP.DataAccess.BonusByRicpt bnsrcpt = new BonusByRicpt();
        ridlist = bnsrcpt.returnList(dateformat);

        GridView1.DataSource = ridlist;
        GridView1.DataBind();

    }
}